import { h } from 'vue';
import { Avatar, Tag, Tooltip, Image } from 'ant-design-vue';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
import { Tinymce } from '/@/components/Tinymce';
import Icon from '/@/components/Icon';
import { getDictItemsByCode } from '/@/utils/dict/index';
import { filterMultiDictText } from '/@/utils/dict/JDictSelectUtil.js';
import { isEmpty } from '/@/utils/is';
import { useMessage } from '/@/hooks/web/useMessage';
const { createMessage } = useMessage();

const render = {
	/**
	 * 渲染列表头像
	 */
	renderAvatar: ({ record }) => {
		if (record.avatar) {
			let avatarList = record.avatar.split(',');
			return h(
				'span',
				avatarList.map((item) => {
					return h(Avatar, {
						src: getFileAccessHttpUrl(item),
						shape: 'square',
						size: 'default',
						style: { marginRight: '5px' },
					});
				})
			);
		} else {
			return h(
				Avatar,
				{ shape: 'square', size: 'default' },
				{
					icon: () => h(Icon, { icon: 'ant-design:file-image-outlined', size: 30 }),
				}
			);
		}
	},
	/**
	 * 根据字典编码 渲染
	 * @param v 值
	 * @param code 字典编码
	 * @param renderTag 是否使用tag渲染
	 */
	renderDict: (v, code, renderTag = false) => {
		let text = '';
		let array = getDictItemsByCode(code) || [];
		let obj = array.filter((item) => {
			return item.value == v;
		});
		if (obj.length > 0) {
			text = obj[0].text;
		}
		return isEmpty(text) || !renderTag ? h('span', text) : h(Tag, text);
	},
	/**
	 * 渲染图片
	 * @param text
	 */
	renderImage: ({ text }) => {
		if (!text) {
			return h(Image, {
				width: 30,
				height: 30,
				src: '',
				fallback:
					'',
			});
		}
		let avatarList = text.split(',');
		return h(
			'span',
			avatarList.map((item) => {
				return h(Image, {
					src: getFileAccessHttpUrl(item),
					width: 30,
					height: 30,
					style: { marginRight: '5px' },
					previewMask: () => {
						return h(Icon, { icon: 'ant-design:eye-outlined', size: 20 });
					},
				});
			})
		);
		//update-end-author:taoyan date:2022-5-24 for:  VUEN-1084 【vue3】online表单测试发现的新问题 41、生成的代码，树默认图大小未改
	},
	/**
	 * 渲染 Tooltip
	 * @param text
	 * @param len
	 */
	renderTip: (text, len = 20) => {
		if (text) {
			let showText = text + '';
			if (showText.length > len) {
				showText = showText.substr(0, len) + '...';
			}
			return h(Tooltip, { title: text }, () => showText);
		}
		return text;
	},
	/**
	 * 渲染a标签
	 * @param text
	 */
	renderHref: ({ text }) => {
		if (!text) {
			return '';
		}
		const len = 20;
		if (text.length > len) {
			text = text.substr(0, len);
		}
		return h('a', { href: text, target: '_blank' }, text);
	},
	/**
	 * 根据字典渲染
	 * @param v
	 * @param array
	 */
	renderDictNative: (v, array, renderTag = false) => {
		let text = '';
		let color = '';
		let obj = array.filter((item) => {
			return item.value == v;
		});
		if (obj.length > 0) {
			text = obj[0].label;
			color = obj[0].color;
		}
		return isEmpty(text) || !renderTag ? h('span', text) : h(Tag, { color }, () => text);
	},
	/**
	 * 渲染富文本
	 */
	renderTinymce: ({ model, field }) => {
		return h(Tinymce, {
			showImageUpload: false,
			height: 300,
			value: model[field],
			onChange: (value : string) => {
				model[field] = value;
			},
		});
	},

	renderSwitch: (text, arr) => {
		return text ? filterMultiDictText(arr, text) : '';
	},
	renderCategoryTree: (text, code) => {
		let array = getDictItemsByCode(code);
		return filterMultiDictText(array, text);
	},
	renderTag(text, color) {
		return isEmpty(text) ? h('span', text) : h(Tag, { color }, () => text);
	},
};

/**
 * 文件下载
 */
function downloadFile(url) {
	if (!url) {
		createMessage.warning('未知的文件');
		return;
	}
	if (url.indexOf(',') > 0) {
		url = url.substring(0, url.indexOf(','));
	}
	url = getFileAccessHttpUrl(url.split(',')[0]);
	if (url) {
		window.open(url);
	}
}
function formattingDay(d) {
	let date = new Date(d);
	let year = date.getFullYear();
	let month = ("0" + (date.getMonth() + 1)).slice(-2);
	let day = ("0" + date.getDate()).slice(-2);
	return year + "-" + month + "-" + day;
}

function formattingMin(d) {
	let date = new Date(d);
	let year = date.getFullYear();
	let month = ("0" + (date.getMonth() + 1)).slice(-2);
	let day = ("0" + date.getDate()).slice(-2);
	let hour = ("0" + date.getHours()).slice(-2);
	let minute = ("0" + date.getMinutes()).slice(-2);
	let second = ("0" + date.getSeconds()).slice(-2);
	let formattedTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
	return formattedTime
}

function formatTimestamp(timestamp) {
	const dateObj = new Date(timestamp * 1);
	const options = {
		year: 'numeric',
		month: '2-digit',
		day: '2-digit',
		hour: '2-digit',
		minute: '2-digit',
		second: '2-digit'
	};

	return dateObj.toLocaleString('zh-CN', options).replace(/\//g, '-');
}

function mainTravelStats(text) {
	if (text == 'WaitOwner') {
		return '待创建人付款'
	} else if (text == 'Freze') {
		return '已下架'
	} else if (text == 'Trash') {
		return '已删除'
	} else if (text == 'Wait') {
		return '报名中'
	} else if (text == 'ApplyOk') {
		return '已成团'
	} else if (text == 'Cancel') {
		return '已取消'
	} else if (text == 'TravelOver') {
		return '行程结束'
	} else if (text == 'WaitTravel') {
		return '待出行'
	}else if (text == 'ApplyForDrawback') {
		return '申请取消行程'
	}
}



function orderTypeReturn(text) {
	if (text == 'Waitpay') {
		return '报名待付款'
	} else if (text == 'Mark') {
		return '候选待付款'
	} else if (text == 'Join') {
		return '已入团'
	} else if (text == 'Payback') {
		return '已退款'
	} else if (text == 'Out') {
		return '已离开'
	} else if (text == 'Timeout') {
		return '超时退出'
	} else if (text == 'Finished') {
		return '已完成'
	} else if (text == 'Dispatched') {
		return '已调度'
	} else if (text == 'Payed') {
		return '已支付'
	} else if (text == 'PayFail') {
		return '付款失败'
	} else if (text == 'WaitEval') {
		return '待评价'
	} else if (text == 'WaitTravel') {
		return '待出行'
	} else if (text == 'Cancel') {
		return '已取消'
	} else if (text == 'Refund') {
		return '申请取消行程并退款'
	} else if (text == 'rerundOne') {
		return '申请部分退款'
	}
}


export { render, downloadFile, formattingDay, formattingMin, formatTimestamp, mainTravelStats, orderTypeReturn };